<?php
/**
 * This file contains the class definition for a SuggestionDAO.
 * The purpose of a SuggestionDAO is to serve as a collection
 * of Suggestion objects, as well as to provide an abstraction
 * of CRUD (create, read, update, delete) actions on the corresponding
 * database table.
 */
include_once("../model/Suggestion.php");
include_once("../dao/BaseDAO.php");
class SuggestionDAO extends BaseDAO {
    
    private $suggestionDatabaseTableName = 'suggestion';
    
    /**
     * Constructor.
     * 
     * Instantiates a SuggestionDAO object.
     * This constructor overrides the one for the parent BaseDAO class.
     * 
     * @param mixed $fetchConditions
     * This is an optional argument to allow data to be fetched selectively
     * at construction, where 
     * (a) If it is a number, this constructor will
     * look for it under the 'id' column in the database table for suggestions.
     * (b) If it is an array, this constructor will treat it as an array of
     * conditions for the SQL statement, SELECT. Ie. given an array where
     * array[$key] = $value, the database will be queried with
     * "SELECT * from $suggestionTable WHERE $key = $value".
     * 
     * @throws Exception
     * if $fetchConditions is not of a type this constructor can
     * handle. Inherited from parent BaseDAO class.
     */
    public function __construct($fetchConditions = NULL) {
        parent::__construct($this->suggestionDatabaseTableName, $fetchConditions);
    }
    
    /**
     * Method: getAllSuggestions
     * 
     * @return array An array of all Suggestion objects from the database.
     */
    public function getAllSuggestions() {
        
        $suggestionList = array();
        
        foreach ($this->fetchedData as $index => $innerArray) {

            // fetchedData exists in parent BaseDAO class as a 2-dimensional array.

            foreach ($innerArray as $key => $value) {
                
                // Gather details of the suggestion currently being iterated through.
                $suggestionId;
                $suggestionTitle;
                $suggestionAuthor;
                $suggestionDescription;

                switch($key) {
                    case "SuggestionId":
                        $suggestionId = $value;
                        break;
                    
                    case "Title":
                        $suggestionTitle = $value;
                        break;
                    
                    case "AccountId":
                        // TODO use AccountID as foreign key in "account" table to retrieve the author.
                        // Author will be NULL in the meantime.
                        break;
                    
                    case "Description":
                        $suggestionDescription = $value;
                }
                
                // -------- debug -----------
                if (is_null($suggestionId)) {
                    echo "Warning: suggestionId detected as NULL at fetchedData[{$index}].";
                }
                // -----  end debug ----------
                
                // Construct the corresponding Suggestion object and populate
                // with the values gathered.
                $suggestion = new Suggestion(
                                $suggestionId,
                                $suggestionTitle,
                                $suggestionAuthor,
                                $suggestionDescription);

                // Store in the array this method will return.
                $suggestionList[$suggestionId] = $suggestion;
            }
        }
        
        return $suggestionList;
    }
    
    public function createSuggestion(&$suggestion) {
        
        if ($suggestion->getContentType() == ContentType::Suggestion) {
            parent::create($suggestion);
            
        } else {
            throw Exception("Content type mis-match, cannot create suggestion. Was given content of type: {$suggestion->getContentType()}");
        }
    }
    
    public function updateSuggestion(&$newSuggestion) {
        
        if ($newSuggestion->getContentType() != ContentType::Suggestion) {
         throw Exception("Content type mis-match, cannot update suggestion. Was given type of type: {$newSuggestion->getContentType()}");   
        }
        
        // If suggestion is valid (id is not 0), but doesn't exist yet,
        // we assume the developer has called this method unintentionally,
        // and proceed to create the suggestion instead.
        if (!isset($this->fetchedData[$newSuggestion->getId()]) //getId shall return an integer.
                && $newSuggestion->getId() != 0) {
            
            $this->createSuggestion($newSuggestion);
            
        } else {
            parent::update($newSuggestion);
        }
    }
    
    public function deleteSuggestion($suggestionId) {
        
        if (isset($this->fetchedData[$suggestionId])) {
            try {
                parent::delete($suggestionId);
                
            } catch (Exception $e) {
                echo $e->getMessage(), "\n";
            }
        }
    }
    
    public function deleteAllSuggestions() {
        try {
            parent::deleteAll();
            
        } catch (Exception $e) {
            echo $e->getMessage(), "\n";
        }
    }
}
?>
